﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Permissions;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Newtonsoft.Json;

namespace DMS.MechanicalReport.ReportUpdater
{
    /// <summary>
    /// List Item Events
    /// 1. when a new item is added
    ///     1.1 check if other documents in the same folder
    ///         true{ rename the document with goodid.pdf to goodid-[current datetime].pdf }
    ///         rename the current item to goodid.pdf
    ///     1.2 retrieve metadata from CRM for this goodid
    ///     1.3 update the current item metadata
    /// </summary>
    public class ReportUpdater : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

       }

       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);
       }

       /// <summary>
       /// An item is being deleted.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);
       }

       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);

       }

        /// <summary>
       /// An item was updated.
       /// </summary>
       public override void ItemUpdated(SPItemEventProperties properties)
       {
           base.ItemUpdated(properties);

           string itemUrl = properties.ListItem.Url;
            var reportId = properties.ListItem["ReportID"];
           if (itemUrl.EndsWith("pdf") && reportId != null)
           {
               string itemPath = itemUrl.Substring(0, itemUrl.LastIndexOf("/", System.StringComparison.Ordinal));
               string goodId = itemPath.Substring(itemPath.LastIndexOf("/", System.StringComparison.Ordinal) + 1);

               string latestItemUrl = properties.WebUrl + "/" + itemPath + "/" + goodId + ".pdf";

               SPWeb web = properties.OpenWeb();
               try
               {
                   SPListItem currentLatestItem = web.GetListItem(latestItemUrl);

                   if (currentLatestItem != null)
                   {
                       string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
                       currentLatestItem["Name"] = goodId + "-" + timeStamp + ".pdf";
                       this.EventFiringEnabled = false;
                       currentLatestItem.Update();
                       this.EventFiringEnabled = true;
                   }
               }
               catch (Exception)
               {
               }

               SPListItem current = properties.ListItem;
               current["Name"] = goodId + ".pdf";
               this.EventFiringEnabled = false;
               current.Update();
               this.EventFiringEnabled = true;
           }
       }

       /// <summary>
       /// An item was deleted.
       /// </summary>
       public override void ItemDeleted(SPItemEventProperties properties)
       {
           base.ItemDeleted(properties);
       }


    }    
    public class MechanicalReportMetaData
    {             
        public int ReportID { get;  set; }  
        public int? ReportVersion { get;  set; }     
        public string BranchName { get;  set; }  
        public string BranchCode { get;  set; }                
        public string Vin { get;  set; }  
        public string Plate { get;  set; }
    }
}
